package com.it.selfAfter.controller;

import com.it.selfAfter.service.ProductService;
import com.it.selfAfter.utils.Result;
import com.mysql.cj.log.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import javax.xml.ws.Response;
import java.util.Map;

/**
 * @Author: Coke
 * @DateTime: 2023/05/25/20:39
 * @注释: 商品
 **/
@CrossOrigin (origins = "*") // 解决跨域问题
@RestController
@RequestMapping ("/api/product")
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;

    @PostMapping("/saveProduct")
    public Result<?> saveProduct(@RequestParam(value = "name") String name,
                                 @RequestParam(value = "productionType") String productionType,
                                 @RequestParam(value = "result") String result,
                                 @RequestParam(value = "address") String address,
                                 @RequestParam(value = "description") String description,
                                 @RequestParam(value = "phone") String phone,
                                 @RequestParam(value = "userId") String userId,
                                 @RequestParam(value = "createName") String createName,
                                 @RequestParam(value = "imgs") MultipartFile[] imgs) {
        // 执行相应的业务逻辑
        return Result.success(productService.saveProduct(name,productionType,result,address,
                description,phone,userId,createName, imgs));
    }


    @PostMapping("/updateProduct")
    public Result<?> updateProduct(@RequestParam(value = "proId") String proId,
                                   @RequestParam(value = "name") String name,
                                   @RequestParam(value = "productionType") String productionType,
                                   @RequestParam(value = "result") String result,
                                   @RequestParam(value = "address") String address,
                                   @RequestParam(value = "description") String description,
                                   @RequestParam(value = "deleteImgs") String deleteImgs,
                                   @RequestParam(value = "noDeleteImg") String noDeleteImg,
                                   @RequestParam(value = "imgs") MultipartFile[] imgs) {
        // 执行相应的业务逻辑
        return Result.success(productService.updateProduct(proId,name,productionType,result,address,
                description,deleteImgs,noDeleteImg,imgs));
    }

    @GetMapping("/getProInfo")
    public Result<?> getProInfo(){
        return Result.success(productService.getProInfo());
    }

    @PostMapping("/queryProductList")
    public Result<?> queryProductList(@RequestBody Map<String, String> query, @PathParam("size") Integer size, @PathParam("page") Integer page) {
        // 处理获取数据的逻辑
//        log.info("开始执行逻辑~~");
        return productService.queryProductList(query, size, page);
    }


    @PostMapping("/getProductInfoById")
    public Result<?> getProductInfoById(@RequestBody Map<String, Object> request) {
        // 处理获取数据的逻辑
        String proId = request.get("proId").toString();
        Long id = Long.valueOf(proId);
        String userId = request.get("userId").toString();
        log.info("开始执行逻辑~~");
        return Result.success(productService.getProductInfoById(id, userId));
//        return Response.ok(productDetailService.getProductDetailInfoById("1593962495", id, whoisVo.getHeadimgurl()));
    }

    @GetMapping("/getProductBaseInfo")
    public Result<?> getProductBaseInfo() {
        // 处理获取数据的逻辑
        log.info("开始执行逻辑~~");
        return Result.success(productService.getProductBaseInfo());
    }

    @PostMapping("/updateCollectById")
    public Result<?> updateCollectById(@RequestBody Map<String, Object> request) {
        String proId = request.get("proId").toString();
        String phone = request.get("phone").toString();
        String userId = request.get("userId").toString();
        // 执行相应的业务逻辑
        return Result.success(productService.updateCollectById(proId,phone,userId));
    }

    @PostMapping("/saveCollectById")
    public Result<?> saveCollectById(@RequestBody Map<String, Object> request) {
        String proId = request.get("proId").toString();
        String phone = request.get("phone").toString();
        String userId = request.get("userId").toString();
        // 执行相应的业务逻辑
        return Result.success(productService.saveCollectById(proId,phone,userId));
    }

    @PostMapping("/echoProductInfoById")
    public Result<?> echoProductInfoById(@RequestBody Map<String, Object> request) {
        String proId = request.get("proId").toString();
        String phone = request.get("phone").toString();
        String userId = request.get("userId").toString();
        // 执行相应的业务逻辑
        return Result.success(productService.echoProductInfoById(proId, phone, userId));
    }
}
